package cn.project.engineering.ui

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import cn.project.engineering.R
import cn.project.engineering.base.MyAppCompatActivity
import cn.project.engineering.http.HttpClient
import cn.project.engineering.http.RequestCallback
import cn.project.engineering.http.bean.*
import cn.project.engineering.ui.adapter.DianjianFourDapter
import cn.project.engineering.ui.adapter.DianjianOneDapter
import cn.project.engineering.ui.adapter.DianjianThreeDapter
import cn.project.engineering.ui.adapter.DianjianTwoDapter
import cn.project.engineering.utils.AppUtils
import cn.project.engineering.utils.IpAddressUtils
import cn.project.engineering.utils.toast
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.google.gson.Gson
import com.hjq.bar.OnTitleBarListener
import kotlinx.android.synthetic.main.activity_dianjian.*
import log.KLog
import org.json.JSONArray

class DianjianActivity : MyAppCompatActivity() {
    companion object {
        private const val TAG = "DianjianActivity"
    }
    lateinit var  dianjiaOneInfo :MutableList<DianjiaOneInfo>;
    lateinit var  dianjianOneDapter: DianjianOneDapter;

    lateinit var  dianjiaTwoInfo :MutableList<DianjiaTwonfo>;
    lateinit var  dianjianTwoDapter: DianjianTwoDapter

    lateinit var  dianjiaThreeInfo :MutableList<DianjiaThreeInfo>;
    lateinit var  dianjianThreeDapter: DianjianThreeDapter

   lateinit var  dianjiaFournfo :MutableList<DianjiaFourInfo>;
    lateinit var  dianjianFourDapter: DianjianFourDapter

    var  FBillNO:String=""
    var time:String=""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dianjian)
        initView()
        initData()

    }

    override fun netWork(isnet: Boolean) {
    }

    fun  initView(){
        titlebar.setOnTitleBarListener(object : OnTitleBarListener{
            override fun onLeftClick(view: View?) {
                finish()
            }

            override fun onRightClick(view: View?) {

            }

            override fun onTitleClick(view: View?) {
            }
        })
        dianjiaOneInfo=ArrayList()
        dianjianOneDapter= DianjianOneDapter(R.layout.item_dianjian_left_top,dianjiaOneInfo)
        left_top_rv.layoutManager= LinearLayoutManager(this)
        left_top_rv.adapter=dianjianOneDapter
        dianjianOneDapter.setOnItemClickListener(OnItemClickListener() { adapter, view, position ->
            if (dianjianOneDapter.data.size==0){
                return@OnItemClickListener
            }
            FBillNO=dianjianOneDapter.data.get(position).FBillNO
            HttpClient.httpService().dianjianTwo( FBillNO= FBillNO).enqueue(object : RequestCallback<MutableList<DianjiaTwonfo>> {
            override fun onRequestSuccess(body: MutableList<DianjiaTwonfo>?) {
                body?.let {
                    dianjiaTwoInfo.clear()
                    if (it.isNotEmpty()) {
                        dianjianTwoDapter.setNewData(it)
                        dianjianTwoDapter.notifyDataSetChanged()

                    }else{

                    }
                }

            }
            override fun onRequestFail(error: RequestError) {

            }

        })

        })




        dianjiaTwoInfo=ArrayList()
        dianjianTwoDapter= DianjianTwoDapter(R.layout.item_dianjian_left_bottom,dianjiaTwoInfo)
        left_bottom_rv.layoutManager= LinearLayoutManager(this)
        left_bottom_rv.adapter=dianjianTwoDapter


        //提交
        left_bottom_submit.setOnClickListener(View.OnClickListener {
            time=AppUtils.currentTime()
            val data = dianjianTwoDapter.data;
            var  jsonArray=JSONArray()
            val size = data.size;
            KLog.d(TAG,"="+size+"--"+data.toString())
               for (i in 0 until size){
                   KLog.d(TAG,"---"+i)
                   var map: HashMap<String, String> = HashMap()
                    map["FBillNO"]=FBillNO
                    map["ChkNO"]=time
                   map["ItemNO"]=data[i].ItemNO
                    map["ItemContent"]=data[i].ItemContent
                   if (data[i].ItemContent.equals("")){
                       toast("点检内容不能为空")
                   }
                    map["Result"]=data[i].Result
                   jsonArray.put(map)

               }
            var  gson=Gson()
            var  json:String= gson.toJson(jsonArray)
            json=json.replace("{\"values\":","")
            json=json.substring(0,json.length-1)
            KLog.d(TAG,json)
            json?.let { it1 ->
                HttpClient.httpService().dianjianTijiao(MacAdd=IpAddressUtils.getMacAddress(),json = json).enqueue(object : RequestCallback<String> {
                    override fun onRequestSuccess(body: String?) {
                        body?.let {
                            dianjiaOneInfo.clear()
                            if (it.isNotEmpty()) {
                                if (it.equals("保存成功")){
                                    getDianjianThree()
                                }else{
                                    toast(it+"")
                                }

                            }else{

                            }
                        }

                    }

                    override fun onRequestFail(error: RequestError) {

                    }

                })
            }

        })


        dianjiaThreeInfo=ArrayList()
        dianjianThreeDapter= DianjianThreeDapter(R.layout.item_dianjian_left_top,dianjiaThreeInfo)
        right_top_rv.layoutManager= LinearLayoutManager(this)
        right_top_rv.adapter=dianjianThreeDapter


        dianjianThreeDapter.setOnItemClickListener { adapter, view, position ->

            var  ChkNO=dianjianThreeDapter.data[position].ChkNO
            HttpClient.httpService().dianjianFour(ChkNO = ChkNO).enqueue(object : RequestCallback<MutableList<DianjiaFourInfo>> {
                override fun onRequestSuccess(body: MutableList<DianjiaFourInfo>?) {
                    body?.let {
                        if (it.isNotEmpty()) {
//                        val apkInfo = it[0]
                            KLog.d("zhengpeng","--"+it.size)
                            dianjianFourDapter.setNewData(it)
                            dianjianFourDapter.notifyDataSetChanged()

                        }else{

                        }
                    }

                }
                override fun onRequestFail(error: RequestError) {

                }

            })
        }



        dianjiaFournfo=ArrayList()
        dianjianFourDapter= DianjianFourDapter(R.layout.item_dianjian_left_bottom_right,dianjiaFournfo)
        left_bottom_right_rv.layoutManager= LinearLayoutManager(this)
        left_bottom_right_rv.adapter=dianjianFourDapter


    }

    fun  initData(){
        HttpClient.httpService().dianjianOne(MacIP = IpAddressUtils.getMacAddress()).enqueue(object : RequestCallback<MutableList<DianjiaOneInfo>> {
            override fun onRequestSuccess(body: MutableList<DianjiaOneInfo>?) {
                body?.let {
                    dianjiaOneInfo.clear()
                    if (it.isNotEmpty()) {
//                        val apkInfo = it[0]
                        KLog.d("zhengpeng","--"+it.size)
                        dianjianOneDapter.setNewData(it)
                        dianjianOneDapter.notifyDataSetChanged()

                    }else{

                    }
                }

            }
            override fun onRequestFail(error: RequestError) {

            }

        })

        getDianjianThree()


    }

    fun getDianjianThree(){

        HttpClient.httpService().dianjianThree(MacIP = IpAddressUtils.getMacAddress()).enqueue(object : RequestCallback<MutableList<DianjiaThreeInfo>> {
            override fun onRequestSuccess(body: MutableList<DianjiaThreeInfo>?) {
                body?.let {
                    dianjiaThreeInfo.clear()
                    if (it.isNotEmpty()) {
//                        val apkInfo = it[0]
                        KLog.d("zhengpeng","--"+it.size)
                        dianjianThreeDapter.setNewData(it)
                        dianjianThreeDapter.notifyDataSetChanged()

                    }else{

                    }
                }

            }
            override fun onRequestFail(error: RequestError) {

            }

        })
    }

}